/*
 * Copyright (c) 2008 Benoit Bouchez, Nicolas Giard
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package com.noocodecommit.wicket.stuff.jquery.plugin.autodefaulttextfield;

import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.IModel;

import com.noocodecommit.wicket.stuff.jquery.JQCore;
import com.noocodecommit.wicket.stuff.jquery.JQOptions;
import com.noocodecommit.wicket.stuff.jquery.exception.CannotApplyJQOptionException;
import com.noocodecommit.wicket.stuff.jquery.plugin.IJQPluginComponent;
import com.noocodecommit.wicket.stuff.jquery.plugin.JQPlugin;

/**
 * @author Nicolas Giard
 */
public class JQAutoDefaultTextField extends TextField implements IJQPluginComponent
{
	/**
	 * JQuery AutoDefaultTextField plugin
	 */
	private JQAutoDefaultTextFieldPlugin plugin;

	/** JQuery AutoDefaultTextField widget options */
	private JQAutoDefaultTextFieldOptions options = new JQAutoDefaultTextFieldOptions();

	/**
	 * @param id
	 */
	public JQAutoDefaultTextField(final String id)
	{
		super(id);

		init(JQAutoDefaultTextFieldPlugin.DEFAULT_KEY);
	}

	/**
	 * @param id
	 * @param key
	 */
	public JQAutoDefaultTextField(final String id, String key)
	{
		super(id);
		init(key);
	}

	/**
	 * @param id
	 * @param object
	 */
	public JQAutoDefaultTextField(final String id, final IModel object)
	{
		super(id, object);

		init(JQAutoDefaultTextFieldPlugin.DEFAULT_KEY);
	}

	/**
	 * @param id
	 * @param key
	 * @param object
	 */
	public JQAutoDefaultTextField(final String id, String key, final IModel object)
	{
		super(id, object);

		init(key);
	}

	/**
	 * @param id
	 * @param type
	 */
	public JQAutoDefaultTextField(final String id, Class type)
	{
		super(id);
		setType(type);

		init(JQAutoDefaultTextFieldPlugin.DEFAULT_KEY);
	}

	/**
	 * @param id
	 * @param key
	 * @param type
	 */
	public JQAutoDefaultTextField(final String id, String key, Class type)
	{
		super(id);
		setType(type);

		init(key);
	}

	/**
	 * @param id
	 * @param model
	 * @param type
	 */
	public JQAutoDefaultTextField(final String id, IModel model, Class type)
	{
		super(id, model);
		setType(type);

		init(JQAutoDefaultTextFieldPlugin.DEFAULT_KEY);
	}

	/**
	 * @param id
	 * @param key
	 * @param model
	 * @param type
	 */
	public JQAutoDefaultTextField(final String id, String key, IModel model, Class type)
	{
		super(id, model);
		setType(type);

		init(JQAutoDefaultTextFieldPlugin.DEFAULT_KEY);
	}

	/**
	 * @param key
	 */
	public void init(String key)
	{
		this.setOutputMarkupId(true);
		if (key == null)
		{
			key = JQAutoDefaultTextFieldPlugin.DEFAULT_KEY;
		}
		// -- Checks plugin existence
		if (JQCore.get().getPlugin(key) == null)
		{
			new JQAutoDefaultTextFieldPlugin(key);
		}
		this.plugin = (JQAutoDefaultTextFieldPlugin) JQCore.get().getPlugin(key);
		JQCore.get().getPlugin(key).attachComponentForDocumentReady(this);
	}

	@Override
	protected void onComponentTag(ComponentTag tag)
	{
		super.onComponentTag(tag);

		JQAutoDefaultTextFieldOptions opts = null;
		if (getPlugin().getInitMode() == JQPlugin.PLUGIN_INIT_ONCE)
			opts = getPlugin().getOptions();
		else
			opts = getOptions();

		if (opts != null)
		{
			if (!opts.getCssClassName().equals(""))
				tag.put("class", opts.getCssClassName());
			if (!opts.getDefaultValue().equals(""))
				tag.put("title", opts.getDefaultValue());
		}
	}

	public String getAfterAjaxUpdateScript()
	{
		return null;
	}

	public JQAutoDefaultTextFieldOptions getOptions()
	{
		return this.options;
	}

	public JQAutoDefaultTextFieldPlugin getPlugin()
	{
		return this.plugin;
	}

	public String getWidgetReadyScript(boolean reset)
	{
		return null;
	}

	public void setOptions(JQOptions options)
	{
		if (options.getClass().equals(JQAutoDefaultTextFieldOptions.class))
		{
			this.options = (JQAutoDefaultTextFieldOptions) options;
		}
		else
		{
			throw new CannotApplyJQOptionException(JQAutoDefaultTextField.class, options.getClass(), JQAutoDefaultTextFieldOptions.class);
		}
	}

	public void setPlugin(JQPlugin plugin)
	{
		this.plugin = (JQAutoDefaultTextFieldPlugin) plugin;
	}
}